package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.IEventDao;
import com.pr.makfilm.domain.Event;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.service.IEventService;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 17.09.13
 * Time: 23:20
 * To change this template use File | Settings | File Templates.
 */
@Service(EventServiceImpl.BEAN_NAME)
public class EventServiceImpl extends AbstractServiceImpl<Event> implements IEventService
{
   public static final String BEAN_NAME = "eventService";

   @Resource
   private IEventDao eventDao;

   @Resource
   private IRoomService roomService;

   @Override
   protected IAbstractDao<Event> getDao()
   {
      return eventDao;
   }

   @Override
   @Transactional
   public Event save(Event obj)
   {
      if (obj.getObjectState() == null)
      {
         obj.setObjectState(ObjectStateEnum.ACTIVE);
      }
      if (obj.getSid() == null)
      {
         obj.setSid(SidUtils.generate());
      }
      if (obj.getRoom() != null)
      {
         obj.setRoom(roomService.getBySid(obj.getRoom().getSid()));
      }
      return super.save(obj);
   }
}
